from typing import List
from dto.strategy_info import StrategyInfoMetadata
from dto.strategy_assessment import RiskAssessment
from dto.strategy_info import StrategyInfoMetadata
from dto.strategy_stock_day import StrategyStockDay
from service.strategy.base_strategy import BaseStrategy


# 实现反向策略类
class ValleyAndRiseStrategy(BaseStrategy):
    """
    判断股票在过去5天是否已经过低谷并且当前处于上涨趋势。
    如果低谷在昨天，则得分3分，每往前一天，分数递减1分。
    """

    def analyze(self, trade_info_list: List[StrategyStockDay]) -> RiskAssessment:
        """
        分析过去5天内是否有低谷，并且判断当前是否处于上涨趋势
        """
        node_point = 0
        description = ""

        # 确保至少有5天的交易数据
        if len(trade_info_list) >= 5:
            # 获取过去5天的股票价格数据
            recent_trades = trade_info_list[-5:]
            lows = [trade.low for trade in recent_trades]
            closes = [trade.close for trade in recent_trades]

            # 判断是否有低谷
            valley_index = lows.index(min(lows))
            valley_value = lows[valley_index]

            # 判断当前是否处于上涨趋势
            if closes[-1] > closes[-2]:  # 当前收盘价大于前一天，说明在上涨
                # 判断低谷在昨天
                if valley_index == 3:  # 如果低谷发生在昨天（即距离最后一个交易日有1天）
                    node_point += 3
                    description = "Valley value was on the previous day, score +3."
                else:
                    # 每往前一天，分数减1
                    node_point += max(0, 3 - valley_index)
                    description = f"Valley value was {valley_index + 1} day(s) ago, score +{max(0, 3 - valley_index)}."

        # 创建策略信息对象
        return RiskAssessment(
            stock_code=trade_info_list[0].stock_code,
            description=description,
            confif=self.strategyConfig,
            node_point=node_point,
        )

    def strategyConfig(self) -> dict:
        """
        返回策略的配置
        """
        return StrategyInfoMetadata(
            strategy_code="VRS",
            strategy_name="Valley and Rise Strategy",
            strategy_group=1,  # 假设是关注型策略
            strategy_type="Valley Rise Trend",
            analysis_day=5,
            strategy_level=2,
        )
